/*!
 * Copyright (C) 2020 - All Rights Reserved by
 * @author : ZhaoYanbo
 * @email  : zyb920@hotmail.com
 * @created: 2020-10-31
 * @version: 1.0.0.0
 *
 */

#include "thumbdatamodel.h"
#include <QFileInfo>

ThumbDataModel::ThumbDataModel(QObject *parent) : QAbstractListModel(parent)
{
    m_hRoleNames[PathRole]       = "path_role";
    m_hRoleNames[NameRole]       = "name_role";

    m_headers << "path" << "name";
}

ThumbDataModel::~ThumbDataModel()
{
    clear();
}

void ThumbDataModel::setSource(const QStringList &list)
{
    clear();

    const int len = list.length();
    if(len > 0) {
        beginInsertRows(QModelIndex(), 0, len-1);
        m_list.append(list);
        endInsertRows();
    }
}

bool ThumbDataModel::insert(int index, const QString &path)
{
    if (index >= 0  && index <= length())
    {
        beginInsertRows(QModelIndex(), index, index);
        m_list.insert(index, path);
        endInsertRows();

        return true;
    }
    return false;
}

bool ThumbDataModel::append(const QString &path)
{
#if 0
    return insert(0, path);
#else
    return insert(length(), path);
#endif
}

bool ThumbDataModel::append(const QStringList &list)
{
    const int len = list.length();
    if(len > 0) {
        const int len2 = m_list.length();
        beginInsertRows(QModelIndex(), len2, len2+len-1);
        m_list.append(list);
        endInsertRows();

        return true;
    }
    return false;
}

bool ThumbDataModel::remove(int index)
{
    if( isValid(index) )
    {
        beginRemoveRows(QModelIndex(), index, index);
        {
            m_list.removeAt(index);
        }
        endRemoveRows();
        return true;
    }
    return false;
}

int ThumbDataModel::remove(const QString &path)
{
    int index = m_list.indexOf(path);
    remove(index);
    return index;
}

void ThumbDataModel::clear()
{
    beginResetModel();
    {
        m_list.clear();
    }
    endResetModel();
}

int ThumbDataModel::length() const
{
    return  m_list.length();
}

bool ThumbDataModel::isValid(int index) const
{
    if(index >= 0 && index < length() )
    {
        return true;
    }
    return false;
}

QString ThumbDataModel::filePath(int index) const
{
    if ( isValid(index)  ) {
        return m_list.at(index);
    }
    return "";
}

QString ThumbDataModel::fileName(int index) const
{
    const QString file_path = filePath(index);
    if(file_path.isEmpty())
        return "";
    return fileName(file_path);
}

QString ThumbDataModel::fileName(const QString &file_path) const
{
    QFileInfo info;
    info.setFile(file_path);
    return info.fileName();
}

bool ThumbDataModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    const int row = index.row();
    if ( isValid(row)  )
    {
        switch (role)
        {
        case PathRole:
            m_list[row] = value.toString();
            break;
        }
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

QVariant ThumbDataModel::data(const QModelIndex &index, int role) const
{
    const int row = index.row() ;
    if (row < 0 || row >= m_list.count())
        return QVariant();

    switch (role)
    {
    case PathRole: return m_list.at(row);
    case NameRole: return fileName( m_list.at(row) );
    default: break;
    }

    return QVariant();
}

QVariant ThumbDataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal){
        return m_headers[section];
    }
    return QAbstractListModel::headerData(section,orientation,role);
}

int ThumbDataModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_list.length();
}

int ThumbDataModel::columnCount(const QModelIndex &) const
{
    return m_headers.length();
}

QHash<int, QByteArray> ThumbDataModel::roleNames() const
{
    return m_hRoleNames;
}
